# --------------------------------------------------------------------------------
#  NoiseTube Web application
#  
#  Copyright (C) 2008-2010 SONY Computer Science Laboratory Paris
#  Portions contributed by Vrije Universiteit Brussel (BrusSense team), 2008-2011
# --------------------------------------------------------------------------------
#  This library is free software; you can redistribute it and/or modify it under
#  the terms of the GNU Lesser General Public License, version 2.1, as published
#  by the Free Software Foundation.
#  
#  This library is distributed in the hope that it will be useful, but WITHOUT
#  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
#  FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
#  details.
#  
#  You should have received a copy of the GNU Lesser General Public License along
#  with this library; if not, write to:
#    Free Software Foundation, Inc.,
#    51 Franklin Street, Fifth Floor,
#    Boston, MA  02110-1301, USA.
#  
#  Full GNU LGPL v2.1 text: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.txt
#  NoiseTube project source code repository: http://code.google.com/p/noisetube
# --------------------------------------------------------------------------------
#  More information:
#   - NoiseTube project website: http://www.noisetube.net
#   - Sony Computer Science Laboratory Paris: http://csl.sony.fr
#   - VUB BrusSense team: http://www.brussense.be
# --------------------------------------------------------------------------------
 
class HomepageController < ApplicationController
  include Geokit::Geocoders

  #caches_action :index, :layout=>false
  before_filter :admin_check, :only => [:backups]

  def index

  end

  def team
    render :layout => "layouts/application"
  end
  
  def publications
    render :layout => "layouts/application"
  end
    
  # POST /invitations POST /invitations.xml
  def invitation
    @invitation = Invitation.new(params[:invitation])
    respond_to do |format|
      if @invitation.save
        format.js{
          # no need rjs file
          render :update do |page|
            page.remove "invitation_form"
            page.replace_html :notice, "Thank you!"
            page.visual_effect :highlight, :notice, :duration => 2 
          end
        }
      else
        format.js{
          render :update do |page| 
            page.replace_html :notice, @invitation.errors.full_messages[0]
            page.visual_effect :highlight, :notice, :duration => 2 
          end 
        }
      end
    end
  end

  def api_overview

  end

  # backups: for the backups, specifically for the iScope people
  def backups
    #todo: send with x_sendfile for faster performance
    @dumps =  Dir.glob(Dir[DB_BACKUPS_PATH])
  end

  # #################### TEST Function ######################

  def map_normalgps
    respond_to do |format|     
      format.kml{
        @tracks=Track.find(:all, :include=>:measures, :conditions => "geolocated=true and count>500")
        render :template=>"/cities/map_normal_gps.kml.erb"
      }
    end
  end
  
  def map_correctedgps
    respond_to do |format|     
      format.kml{
        city=City.find(:first,:conditions=>{:name=>"Paris", :country=>"FR"})
        @tracks=city.tracks.find(:all,:conditions=>"geolocated=true")
        render :template=>"/cities/map_corrected_gps.kml.erb"
      }
    end
  end

  private
  def admin_check
    if current_user.nil? or (current_user.role != "iscopeadmin" and current_user.role != "admin")
      redirect_to root_url, :notice => (t 'messages.security.notauthorized')
    end
  end

end
